package top.kaoshanji.p1ch03a;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.util.Date;

/**
 * 代码清单-p1ch03a-64页c：表示订单的类
 * author kaoshanji
 * ^_^
 * create time 2025/2/17 11:01
 */
public class Order {

    private static final Logger logger = LogManager.getLogger(Order.class);

    public static void main(String[] args) {

        logger.info("...代码清单-p1ch03a-64页c：表示订单的类...");

        // 用户对象
        User user = new User();
        user.setName("你的名字");
        user.setPassword("你的密码");


        // 订单条目数组
        OrderItem[] orderItems = new OrderItem[2];

        // 商品对象1
        Product product1 = new Product();
        product1.setId("1");
        product1.setName("1号");
        product1.setPrice(1.1d);

        // 订单条目对象
        OrderItem orderItem1 = new OrderItem(product1, 11);
        orderItems[0] = orderItem1;

        // 商品对象2
        Product product2 = new Product();
        product2.setId("2");
        product2.setName("2号");
        product2.setPrice(2.1d);

        // 订单条目对象
        OrderItem orderItem2 = new OrderItem(product2, 22);
        orderItems[1] = orderItem2;


        // 订单对象
        Order order = new Order(); // 创建对象
        // 为属性字段赋值，包含了实体之间的关系
        order.setId("订单号1");
        order.setUser(user);
        order.setItems(orderItems);

        // 调用对象方法
        logger.info("...订单商品总价:{}...", order.computeTotalPrice());

    }


    // 订单号
    private String id;

    // 购买的用户
    private User user;

    // 购买产品列表及数量
    private OrderItem[] items;

    // 下单时间
    private Date createTime;

    // 收货人
    private String  receiver;

    // 收货地址
    private String address;

    // 联系电话
    private String phone;

    // 订单状态
    private String status;

    // 计算订单商品总价
    public double computeTotalPrice() {
        double totalPrice = 0;
        if (null != items) {
            for (OrderItem item: items) {
                totalPrice += item.computePrice();
            }
        }
        return totalPrice;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    public OrderItem[] getItems() {
        return items;
    }

    public void setItems(OrderItem[] items) {
        this.items = items;
    }

    public Date getCreateTime() {
        return createTime;
    }

    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }

    public String getReceiver() {
        return receiver;
    }

    public void setReceiver(String receiver) {
        this.receiver = receiver;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }
}
